/***************************************************************************
| File: commands.c  -  Conjuntos de comandos do sistema
|
| Autor: Joao Carlos Bittencourt, Vinicius Bittencourt, Italo Santos (IST)
| Data:  Maio 2012
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cyg/io/io.h>
#include <cyg/kernel/kapi.h>
#include "../include/constrains.h"

#define TEST 0x41

extern Cyg_ErrNo err;
extern cyg_io_handle_t serH;
extern cyg_handle_t mbx1H, mbx2H, mbx3H;


int cmd_err;
extern int nr; /* number of registers to return */
extern int debug;

/*-------------------------------------------------------------------------+
| Function: cmd_exit - termina a aplicacao
+--------------------------------------------------------------------------*/ 
void cmd_exit (int argc, char **argv)
{
  exit(0);
}

/*-------------------------------------------------------------------------+
| Function: cmd_test - apenas como exemplo
+--------------------------------------------------------------------------*/ 
void cmd_test (int argc, char** argv)
{
  int i;

  /* exemplo -- escreve argumentos */
  for (i=0; i<argc; i++)
    printf ("\nargv[%d] = %s", i, argv[i]);
}

/*-------------------------------------------------------------------------+
| Function: cmd_ini - inicializar dispositivo
+--------------------------------------------------------------------------*/ 
void cmd_ini(int argc, char **argv)
{
  printf("io_lookup\n");
  if ((argc > 1) && (argv[1][0] = '1'))
    err = cyg_io_lookup("/dev/ser1", &serH);
  else err = cyg_io_lookup("/dev/ser0", &serH);
  printf("lookup err=%x\n", err);
}

/*-------------------------------------------------------------------------+
| Function: cmd_rc - realiza leitura do relogio
+--------------------------------------------------------------------------*/ 
void cmd_rc(int argc, char **argv) 
{
  unsigned int n, i;
  unsigned char bufw[50];
  unsigned char *bufr;

  if ((n=argc) > 1) {
    n--;
    if (n > 50) n = 50;
    for (i=0; i<n; i++)
      //      sscanf(argv[i+1], "%hhx", &bufw[i]);
    {
	unsigned int x; sscanf(argv[i+1], "%x", &x); bufw[i]=(unsigned char)x;
    }
	nr = 8;
      cyg_mbox_put( mbx1H, bufw);
      bufr = cyg_mbox_get( mbx2H );
	for(i = 0; i < nr; i++)
		printf("n=0x%X \n", bufr[i]);
	//printf("n=0x%X \n", bufr[1]);
  }
  else {
    printf("nenhuma mensagem!!!\n");
  }
 
	
}

void cmd_sc (int argc, char **argv)
{
  unsigned int n=1;
  char bufr[50];

  //if (argc > 1) n = atoi(argv[1]);
  //if (n > 50) n = 50;
  err = cyg_io_read(serH, bufr, &n);
  printf("io_read err=%x, n=%d buf=%s\n", err, n, bufr);
}

/*-------------------------------------------------------------------------+
| Function: cmd_sc - define relogio
+--------------------------------------------------------------------------*/ 
/*void cmd_sc(int argc, char **argv)
{
  printf("In this command you need to insert the new clock time as args.\n");
}*/

/*-------------------------------------------------------------------------+
| Function: cmd_rtl - realiza a leitura da temperatura e luminosidade
+--------------------------------------------------------------------------*/ 
void cmd_rtl(void)
{
  printf("| Reading temperature and luminosity |\n");

  unsigned int i;
  unsigned char message[] = {SOM, RTL, EOM};
  unsigned char *bufr; 

  /* sending the message */
  /*message[0] = SOM;
  message[1] = RTL;
  message[2] = EOM;*/

  if(debug)
  {
    printf("\n>> Initial Package\n");
    for (i = 0; i < 3; ++i)
      printf(":: message[%d]: 0x%X\n", i, message[i]);
  }

  cyg_mbox_put( mbx1H, message );

  /* receiving the returing message */
  nr = 5;
  bufr = cyg_mbox_get( mbx2H );
  if(debug)
  {
    nr = strlen(bufr);
    if(nr > 0) 
    {
      printf("\n>> Response\n");
      for(i = 0; i < nr; i++)
        printf(" :: bufr[%d]=0x%X \n", i, bufr[i]);
    }
    else 
      printf("\n :: no returing message!!\n");
  }
  if(bufr[0] == SOM)
  {
    if(bufr[1] == RTL)
    {
      if(bufr[2] != CMD_ERROR)
      {
        printf("Temperature: %d\n", bufr[2]);
        printf("Luminosity: %d\n", bufr[3]);
      }
      else printf("::: Command error!\n");
    }
    else printf("::: Wrong returing command!\n");
  }
  else printf("::: Start of message not detected\n");
}

/*-------------------------------------------------------------------------+
| Function: cmd_mpm - modifica o periodo de monitoramento
+--------------------------------------------------------------------------*/ 
void cmd_mpm(int argc, char **argv)
{
  printf("Modify monitoring period (seconds - 0 deactivate)\n");
}

/*-------------------------------------------------------------------------+
| Function: cmd_mpm - modifica o periodo de inatividade
+--------------------------------------------------------------------------*/ 
void cmd_mti(int argc, char **argv)
{
  printf("Modify inactivity time (minutes - 0 deactivate)\n");
}

/*-------------------------------------------------------------------------+
| Function: cmd_mpm - le alarmes
+--------------------------------------------------------------------------*/ 
void cmd_ra(void)
{
  printf("Reading alarms\n");
}

/*-------------------------------------------------------------------------+
| Function: cmd_mpm - define hora do alarme
+--------------------------------------------------------------------------*/ 
void cmd_da(int argc, char **argv)
{
  printf("Define alarm clock\n");
}

/*-------------------------------------------------------------------------+
| Function: cmd_mpm - define temperatura de alarme
+--------------------------------------------------------------------------*/ 
void cmd_dt(int argc, char **argv)
{
  printf("Define temperature limit.\n");
}

/*-------------------------------------------------------------------------+
| Function: cmd_mpm - define luminosidade de alarme
+--------------------------------------------------------------------------*/ 
void cmd_dl(int argc, char **argv)
{
  printf("Define light sensor limit.\n");
}

/*-------------------------------------------------------------------------+
| Function: cmd_mpm - activate/deactivate alarms
+--------------------------------------------------------------------------*/ 
void cmd_aa(void)
{
  printf("Activate/deactivate alarms\n");
}

/*-------------------------------------------------------------------------+
| Function: cmd_mpm - information about registers
+--------------------------------------------------------------------------*/ 
void cmd_ir(void)
{
  printf("Get information about registers\n");
}

/*-------------------------------------------------------------------------+
| Function: cmd_mpm - transfere n registros a partir da posicao de leitura atual
+--------------------------------------------------------------------------*/ 
void cmd_trc(int argc, char **argv)
{
  printf("Transfer n registers from current iread position.\n");
}

/*-------------------------------------------------------------------------+
| Function: cmd_mpm - transfere n registros a partir do indice i
+--------------------------------------------------------------------------*/ 
void cmd_tri(int argc, char **argv)
{
  printf("Transfer n registers from index i.\n");
}
